<template>
  <section>
    <!--高级查询工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="AdvancedSearch">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>


    <!--列表-->
    <el-table @row-dblclick="getGrid" :data="dictionary" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="100"  label="编号">
      </el-table-column>
      <el-table-column prop="name" label="数据字典名称" width="300">
      </el-table-column>
      <el-table-column prop="sn" label="数据字典编号" >
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="total,sizes,prev, pager, next"
                     @current-change="handleCurrentChange"
                     :current-page="currentPage"
                     :page-size="pageSize"
                     :page-sizes="[2, 4, 6, 8]"
                     @size-change = "handlePageSize"
                     :total="total"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title"  :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="数据字典名称" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="数据字典编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--抽屉代码-->
    <el-drawer
        title="数据字典明细"
        :visible.sync="table"
        direction="rtl"
        size="50%">
      <el-table :data="gridData" highlight-current-row v-loading="listGridLoading" @selection-change="selsChange"
                style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="100"  label="编号">
        </el-table-column>
        <el-table-column prop="name" label="数据字典明细名称" width="300">
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot="header" slot-scope="scope">
            <el-button size="small" @click="gridAdd(scope.$index, scope.row)">新增</el-button>
          </template>
          <template scope="scope">
            <el-button size="small" @click="gridEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="gridDel(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--抽屉分页工具条-->
      <el-col :span="24" class="toolbar">
        <el-pagination layout="total,sizes,prev, pager, next"
                       @current-change="detailCurrentChange"
                       :current-page="currentPageGrid"
                       :page-size="pageSizeGrid"
                       :page-sizes="[2, 4, 6, 8]"
                       @size-change = "detailPageSize"
                       :total="totalGrid"
                       style="float:right;">
        </el-pagination>
      </el-col>
    </el-drawer>

    <!--抽屉新增/修改界面-->
    <el-dialog :title="titleGrid" :visible.sync="editGridFormVisible" :close-on-click-modal="false">

      <el-form :model="editGridForm" label-width="90px" :rules="editGridFormRules" ref="editGridForm">
        <el-form-item label="明细名" prop="name">
          <el-input v-model="editGridForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="明细类型" prop="sn" hidden>
          <el-input v-model="editGridForm.types_id" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editGridFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editGridSubmit" :loading="editGridLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import { getDepartmentListPage, removeDepartment, batchRemoveDepartment, editDepartment, addDepartment } from '../../api/api';


export default {
  data() {
    return {
      gridData:[],
      type_id:null,
      typeId:null,
      table:false,
      options:[],
      filters: {
        keyword: ''
      },
      dictionary: [],
      total: 0,
      currentPage: 1,
      pageSize: 2,
      // 抽屉的分页参数
      totalGrid: 0,
      currentPageGrid: 1,
      pageSizeGrid: 2,
      listLoading: false,
      listGridLoading:false,
      sels: [],//列表选中列
      title:"",
      titleGrid:"",
      //编辑界面是否显示
      editFormVisible: false,
      editLoading: false,
      editFormRules: {
        name: [
          { required: true, message: '请输入数据字典名称', trigger: 'blur' }
        ],
        sn: [
          { required: true, message: '请输入数据字典编号', trigger: 'blur' }
        ]
      },
      //编辑界面数据
      editForm: {
        id:null,
        name:"",
        sn:""
      },
      //抽屉编辑界面是否显示
      editGridFormVisible: false,
      editGridLoading: false,
      editGridFormRules: {
        name: [
          { required: true, message: '请输入字典明细名称', trigger: 'blur' }
        ]
      },
      //抽屉编辑界面数据
      editGridForm: {
        id:null,
        name:"",
        types_id:this.type_id
      },
      cities:[]
    }
  },
  methods: {
    // 打开抽屉
    getGrid(row){
      // 获取类型id
      this.type_id = row.id;
      // 调用抽屉查询全部
      this.getDetail();
      // 打开抽屉
      this.table = true;
    },
    // 抽屉查询全部
    getDetail(){
      let para = {
        currentPage: this.currentPageGrid,
        pageSize: this.pageSizeGrid,
        keyword: this.filters.keyword,
        typeId: this.type_id
      };
      this.listLoading = true;
      this.$http.post("/detail",para)
          .then(result=>{
            this.listLoading = false;
            this.gridData = result.data.rows;
            this.totalGrid = result.data.total;
          })
          .catch(result=>{
            this.$message.error("系统繁忙,请骚后重试");
            console.log(result)
          })
    },
    // 抽屉分页，换页
    detailCurrentChange(val){
      this.currentPageGrid = val;
      this.getDetail();
    },
    // 抽屉分页展示，更改展示条数
    detailPageSize(val){
      this.currentPageGrid = 1;
      this.pageSizeGrid = val;
      this.getDetail();
    },
    // 抽屉修改页面
    gridEdit: function (index, row) {
      this.editGridFormVisible = true;
      this.titleGrid = "编辑";
      //拷贝
      this.editGridForm = Object.assign({}, row);
      //重置表单，可以清空表单的校验
      this.$refs['editGridForm'].resetFields();
    },
    gridAdd: function () {
      this.editGridFormVisible = true;
      this.titleGrid = "新增";
      this.editGridForm = {
        id:null,
        name:"",
        types_id:this.type_id
      };
      //重置表单，可以清空表单的校验
      this.$refs['editGridForm'].resetFields();
    },
    // 抽屉新增/修改提交
    editGridSubmit: function () {
      this.$refs.editGridForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editGridLoading = true;
            let para = Object.assign({}, this.editGridForm);
            this.$http.put("/detail",para)
                .then(result=>{
                  this.editGridLoading = false;
                  this.editGridFormVisible = false;
                  if(result.data.success){
                    this.$message({
                      message: result.data.message,
                      type: 'success'
                    });
                  }else {
                    this.$message({
                      message: result.data.message,
                      type: 'warning'
                    });
                  }

                  this.getDetail();
                })
                .catch(result=>{
                  this.editLoading = false;
                  this.$message({
                    message: "新增失败",
                    type: 'error'
                  });
                })
          });
        }
      });
    },
    // 抽屉删除
    gridDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listGridLoading = true;
        //NProgress.start();
        this.$http.delete("/detail/"+row.id)
            .then(result=>{
              this.listGridLoading = false;
              if(result.data.success){
                this.$message({
                  message: result.data.message,
                  type: 'success'
                });
              }else {
                this.$message({
                  message: result.data.message,
                  type: 'warning'
                })
              }
              //跳到第一页
              this.currentPageGrid = 1;
              //调用查询列表函数
              this.getDetail();
            })
            .catch(result=>{
              this.listLoading = false;
              this.$message({
                message: "删除失败",
                type: 'error'
              })
            })
      }).catch(() => {

      });
    },
    
    /*   上面是抽屉功能，下面是外边的功能  */
    //高级查询过后跳转查询出来的数据的第一页
    AdvancedSearch(){
      this.currentPage = 1;
      this.getDictionary();
    },
    // 分页展示数据，换页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getDictionary();
    },
    // 分页展示数据，更改展示条数
    handlePageSize(val){
      this.currentPage = 1;
      this.pageSize = val;
      this.getDictionary();
    },
    //获取用户列表
    getDictionary() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.filters.keyword
      };
      this.listLoading = true;
      this.$http.post("/dic",para)
          .then(result=>{
            this.listLoading = false;
            this.dictionary = result.data.rows;
            this.total = result.data.total;
          })
          .catch(result=>{
            this.$message.error("系统繁忙,请骚后重试");
            console.log(result)
          })
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        this.$http.delete("/dic/"+row.id)
            .then(result=>{
              this.listLoading = false;
              if(result.data.success){
                this.$message({
                  message: result.data.message,
                  type: 'success'
                });
              }else {
                this.$message({
                  message: result.data.message,
                  type: 'warning'
                })
              }
              //跳到第一页
              this.currentPage = 1;
              //调用查询列表函数
              this.getDictionary();
            })
            .catch(result=>{
              this.listLoading = false;
              this.$message({
                message: "删除失败",
                type: 'error'
              })
            })
      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      this.title = "编辑";
      //拷贝
      this.editForm = Object.assign({}, row);
      //重置表单，可以清空表单的校验
      this.$refs['editForm'].resetFields();
    },
    //显示新增界面
    handleAdd: function () {
      this.editFormVisible = true;
      this.title = "新增";
      this.editForm = {
        id:null,
        name:"",
        sn:""
      };
      //重置表单，可以清空表单的校验
      this.$refs['editForm'].resetFields();
    },
    //编辑
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            let para = Object.assign({}, this.editForm);
            this.$http.put("/dic",para)
                .then(result=>{
                  this.editLoading = false;
                  this.editFormVisible = false;
                  if(result.data.success){
                    this.$message({
                      message: result.data.message,
                      type: 'success'
                    });
                  }else {
                    this.$message({
                      message: result.data.message,
                      type: 'warning'
                    });
                  }

                  this.getDictionary();
                })
                .catch(result=>{
                  this.editLoading = false;
                  this.$message({
                    message: "新增失败",
                    type: 'error'
                  });
                })
          });
        }
      });
    },

    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/dic",ids)
            .then(result=>{
              this.listLoading = false;
              if(result.data.success){
                this.$message({
                  message: result.data.message,
                  type: 'success'
                });
              }else {
                this.$message({
                  message: result.data.message,
                  type: 'warning'
                })
              }
              //跳到第一页
              this.currentPage = 1;
              //调用查询列表函数
              this.getDictionary();
            })
            .catch(result=>{
              this.listLoading = false;
              this.$message({
                message: "删除失败",
                type: 'error'
              })
            })
      }).catch(() => {

      });
    }
  },
  mounted() {
    //获取部门列表
    this.getDictionary();

  }
}

</script>

<style scoped>


</style>